package no.rodland.acc.action;

import java.util.List;

import no.rodland.acc.model.Bank;
import no.rodland.acc.model.Budget;
import no.rodland.acc.db.StoreService;
import no.rodland.acc.db.FetchService;

import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by IntelliJ IDEA.
 *
 * @Since: Aug 2, 2010 3:38:13 PM
 * @Author: fmr
 */
public class MatchAction extends ActionSupport {
    private StoreService storeService;
    private FetchService fetchService;
    private Bank bank;
    private List<Budget> budgets;
    private Budget budget;

    public MatchAction(FetchService fetchService, StoreService storeService) {
        this.fetchService = fetchService;
        this.storeService = storeService;
    }

    @Override
    public String execute() {
        if (budget == null) {
            bank = fetchService.getNextBank();
            budgets = fetchService.getBudgets(getBank());
        }
        else if (bank != null) {
            storeService.match(bank, budget);
            bank = fetchService.getNextBank();
            budgets = fetchService.getBudgets(getBank());
        }
        else {
            return ERROR;
        }
        return SUCCESS;
    }

    public Bank getBank() {
        return bank;
    }

    public List<Budget> getBudgets() {
        return budgets;
    }

    public void setBudget(Budget budget) {
        this.budget = budget;
    }
}
